package src

import (
	"database/sql"
	"fmt"
	"github.com/Unknwon/goconfig"
	_ "github.com/go-sql-driver/mysql"
	"log"
)

var (
	cfg, _ = goconfig.LoadConfigFile("./config.ini");
	name, _      = cfg.GetValue("mysql", "username");
	password,_ = cfg.GetValue("mysql", "password");
	url,_ = cfg.GetValue("mysql", "url");
	Db, Err = sql.Open("mysql", name+":"+password+"@tcp"+url+"?parseTime=true");
)

func InitDataBase() {
	if Err != nil { //如果是空的话打印错误信息
		log.Fatalln(Err);
	}
	fmt.Println(password);
	fmt.Println(url);
	//defer Db.Close();       //推迟关闭数据库
	Db.SetMaxIdleConns(20); //最大连接数目
	Db.SetMaxOpenConns(20);
	if err := Db.Ping(); err != nil {
		log.Fatalln(err);
	}
}
